// hooks/useAuth.ts
import { useState, useEffect } from 'react'
import { getStorageSync, redirectTo } from '@tarojs/taro'

export function useAuth(requireAuth = true) {
  const [isChecking, setIsChecking] = useState(true)

  useEffect(() => {
    const checkAuth = async () => {
      const token = getStorageSync('token')
      console.log(`当前token: ${token}`);
      if (requireAuth && !token) {
        // 需要登录但未登录，跳转到登录页
        redirectTo({ url: '/pages/login/index' })
      } else {
        // 已登录或不需要登录，显示内容
        setIsChecking(false)
      }
    }

    checkAuth()
  }, [requireAuth])

  return { isChecking }
}
